package com.sanctuary.services;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sanctuary.calais.client.CalaisLocator;
import com.sanctuary.calais.client.CalaisSoap12Stub;
import com.sanctuary.interfaces.Product;
import com.sanctuary.interfaces.TemplateEngine;
import com.sanctuary.models.SolrResponse;
import com.sanctuary.products.HtmlProduct;
import com.sanctuary.tools.BaseTool;
import com.sanctuary.tools.HttpTools;
import com.sanctuary.tools.SolrTools;
import com.sanctuary.tools.StringTools;
import com.sanctuary.tools.XmlTools;

public class PostItem extends BaseService {
	public static final Log LOG = LogFactory.getLog(PostItem.class);
	private String productArg;
	private static CalaisSoap12Stub calais;
	
	static {
		try {
			calais = (CalaisSoap12Stub) new CalaisLocator().getcalaisSoap12();
		} catch (Exception ex) {
			LOG.fatal("Could not creating Calais binding");
		}
	}
	
	public PostItem(String productArg) {
		this.productArg = productArg;
	}
	@Override
	public Product execute() throws Exception {
		String url = request.getParameter("url");
		String title = request.getParameter("title");
		String content = null;
		Product p = new HtmlProduct();
		p.setWriter(writer);
		String resp = null;
		String _content = "";
		try {
			content = HttpTools.parseSite(url,"body");
			
			// lets take our content and title and feed it into Calais
			StringBuffer calaisInput = new StringBuffer();
			calaisInput.append(title);
			calaisInput.append(" "); // add some space so calais can properly tokenize the input
			calaisInput.append(content);
			
			// lets get our calais parameters XML from our template
			String paramTemplate = BaseTool.CONFIG_BEAN.getCalaisParamTemplate();
			// setup our dynamic parameters:
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("externalId", System.currentTimeMillis());
			params.put("submitter", "anon");
			
			TemplateEngine te = (TemplateEngine) BaseTool.IOC_ENGINE.getBean("templateEngine");
			StringWriter w = (StringWriter) te.getContent(paramTemplate, params);
			String paramXml = w.getBuffer().toString();
			LOG.debug("calais params: " + paramXml);
			// license key, content, params XML
	        resp = calais.enlighten(BaseTool.CONFIG_BEAN.getCalaisLicenseKey(), 
	        		calaisInput.toString(), 
	        		paramXml);
	        List<String> groups = XmlTools.getGroup(resp);
	        
	        // now to use this list to search against Solr
	        List<SolrResponse> results = SolrTools.score(groups);
	        _content = results.toString();
	        
		} catch (Exception ex) {
			LOG.error(StringTools.getStackTrace(ex));
		}
		Map<String,Object> o = new  HashMap<String,Object>();
		o.put("content", _content);
		o.put("url", url);
		
		p.setCompleted(productArg, request, response, template, o);
		
		return p;
	}

	@Override
	public boolean isWriter() {
		// TODO Auto-generated method stub
		return true;
	}

}
